<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>EbookAZ</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<?php require "../components/head.php" ?>
</head>
<body onload="MakeCurrentPage(1)">
<div id="container">
	<?php require "../components/guest_menu.php" ?>
	<?php require "../components/guest_header.php" ?>
	<div id="content">
		<?php require "../components/content_left.php"?>
		<div id="content_right" align="center">
		<?php
		
		require_once "Mail.php";
			
			//Dang ki
			 
		if(isset($_REQUEST['txtEmail'])&& isset($_REQUEST['txtUsername']))
		{
			include "../common/db.inc";
			$mysqli = new mysqli($host,$username,$password,$dbname);
			$mysqli->set_charset("utf8");
			if(mysqli_connect_errno())
			{
				echo "Connect failed: ".mysqli_connect_errno();
				return;
			}
			$email = $mysqli->real_escape_string($_REQUEST['txtEmail']);
			$userName = $mysqli->real_escape_string($_REQUEST['txtUsername']);				
			
				$sql="CALL sp_GetUserByName('$userName')";
				
				$result = @$mysqli->query($sql);

				if (!$mysqli->errno) 
				{
					if($result->num_rows == 1)
					{
						$row = $result->fetch_object();
						$letter_pass = md5(rand(0,999));
						$num_pass = rand(0, 24);
						$pass = substr($letter_pass,$num_pass,8);
						$md5pass = md5($pass);
						$userId = $row->ID;
						$mysqliUpdate = new mysqli($host,$username,$password,$dbname);
						$mysqliUpdate->set_charset("utf8");
						$sql="CALL sp_UpdateUserPass('$userId','$md5pass')";					
						$mysqliUpdate->query($sql);
						
						$smtphost = "ssl://smtp.gmail.com"; 
						$port = 465; 
						$un = "ebookaz2006@gmail.com";
						$pw = "a23d15h18d24d27";	
						
						$subject = "Ebookaz.com - Lấy lại mật khẩu- Recovery Password";
						$message = "Mật khẩu mới của bạn là: $pass";
						$message = str_replace("\n.", "\n..", $message);
						$from = "ebookaz2006@gmail.com";
										
						$headers = array('From'=>$from,'Subject'=>$subject);
						$params = array('host'=>$smtphost,'port'=>$port,'auth'=>true,'username'=>$un,'password'=>$pw);
					
						$smtpMail =& Mail::factory('smtp',$params);
						
						$mail = $smtpMail->send($email,$headers,$message);
						if(PEAR::isError($mail))
						{
							echo "Không thể gửi mail!<br/>";
							echo "<a href='../common/quenmatkhau.php'>Trở về trang lấy lại mật khẩu</a>";

							$mysqli->close();
						}
						else
						{
							echo "Lấy lại mật khẩu thành công!<br/>";
							echo "Vui lòng kiểm tra email để lấy mật khẩu mới!<br/>";
							echo "<a href='../common/home.php'>Trở về trang chủ</a></a>";
						}//end if
					}
					else
					{
						echo "Không tồn tại tên đăng nhập này.<br/>";
						echo "<a href='../common/quenmatkhau.php'>Trở về trang lấy lại mật khẩu</a>";
						$mysqli->close();
					}
				}
				else
				{
					echo "Kết nối bị lỗi.<br/>";
					echo "<a href='../common/quenmatkhau.php'>Trở về trang lấy lại mật khẩu</a>";
					$mysqli->close();
				}//end if		
			
		}//end if
        ?>
		</div>
	</div>
	<?php require "../components/footer.php" ?>
</div>
</body>
</html>
